let sql = require('../sql/sql');
let data = require('../libs/data');
var db = require('../libs/db');
var utils = require('../libs/utils');
var shortId = require('shortid');
var Topic = db.Topic;
var Code = db.code;
var Activity = db.Activity;
var Promsise = require('bluebird');
var jwt = require("jsonwebtoken");
var client = require('redis').createClient();
function randomWord(randomFlag, min, max) {
    var str = "",
        range = min,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        // arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

    // 随机产生
    if (randomFlag) {
        range = Math.round(Math.random() * (max - min)) + min;
    }
    for (var i = 0; i < range; i++) {
        pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}
async function getCode() {
    var code = randomWord(false, 6);
    var rows = await Activity.findOne({
        where: {
            code: code
        }
    });
    console.log("是否为空：",rows==null);
    if(rows==null){
        return code;
    }else{
        return getCode();
    }
}

module.exports = {
    getrcode(){
        var code = [];
        for(i=0;i<3000;i++){
            var codeData = {
                code: randomWord(false, 6),
            };
            Code.create(codeData);
        }
        data.code = '200';
        data.msg = '添加数据';
        res.status(200).send(JSON.stringify(data));
    },
};